  <template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="计划名称">
              <el-input
                v-model="query.planname"
                placeholder="计划名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="巡检人员">
              <el-select
                v-model="query.pollperson"
                placeholder="巡检人员"
                clearable
              >
                <el-option
                  v-for="(item, index) in pollpersonOptions"
                  :key="index"
                  :label="item.F_RealName"
                  :value="item.F_Id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="巡线点">
              <el-select
                v-model="query.pollpoint"
                placeholder="巡线点"
                clearable
              >
                <el-option
                  v-for="(item, index) in pollpointOptions"
                  :key="index"
                  :label="item.DWMC"
                  :value="item.Id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <div class="JNPF-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
            >
              新增
            </el-button>
          </div>
          <div class="JNPF-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JNPF-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screenfull :isContainer="true" />
            <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options JNPF-common-head-icon"
                :underline="false"
              />
            </el-tooltip>
          </div>
        </div>
        <JNPF-table v-loading="listLoading" :data="list">
          <el-table-column prop="xh" label="排序" align="left" />
          <el-table-column prop="planname" label="计划名称" align="left" />
          <el-table-column prop="starttime" label="巡检开始时间" align="left" />
          <el-table-column prop="endtime" label="巡检结束时间" align="left" />
          <el-table-column label="巡检人员" prop="pollperson" algin="left">
            <template slot-scope="scope">{{
              scope.row.pollperson | dynamicText(pollpersonOptions)
            }}</template>
          </el-table-column>
          <el-table-column prop="num" label="巡检次数" align="left" />
          <el-table-column label="巡检类型" prop="cycletype" algin="left">
            <template slot-scope="scope">{{
              scope.row.cycletype | dynamicText(cycletypeOptions)
            }}</template>
          </el-table-column>

          <el-table-column label="巡线点" prop="pollpoint" algin="left">
            <template slot-scope="scope">{{
              scope.row.pollpoint | dynamicText(pollpointOptions)
            }}</template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">
                编辑
              </el-button>
              <el-button
                type="text"
                @click="handleDel(scope.row.id)"
                class="JNPF-table-delBtn"
              >
                删除
              </el-button>
              <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.id, true)"
              >
                详情
              </el-button>
            </template>
          </el-table-column>
        </JNPF-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JNPF-Form v-if="formVisible" ref="JNPFForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
  </div>
</template> 
  <script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import JNPFForm from "./Form";
import ExportBox from "./ExportBox";
import { previewDataInterface } from "@/api/systemData/dataInterface";

export default {
  components: { JNPFForm, ExportBox },
  data() {
    return {
      showAll: false,
      query: {
        planname: undefined,
        pollperson: undefined,
        pollpoint: undefined,
      },
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,

        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "xh", label: "排序" },
        { prop: "planname", label: "计划名称" },
        { prop: "starttime", label: "巡检开始时间" },
        { prop: "endtime", label: "巡检结束时间" },
        { prop: "pollperson", label: "巡检人员" },
        { prop: "cycletype", label: "巡检类型" },
        { prop: "num", label: "巡检次数" },
        { prop: "pollpoint", label: "巡线点" },
      ],
      pollpersonOptions: [],
      cycletypeOptions: [
        { fullName: "年", id: "年" },
        { fullName: "月", id: "月" },
        { fullName: "周", id: "周" },
        { fullName: "日", id: "日" },
      ],
      pollpointOptions: [],
    };
  },
  computed: {},
  created() {
    this.initData();
    // this.getPollpersonOptions();
    // this.getPollpointOptions();
  },
  methods: {
    getPollpersonOptions() {
      return previewDataInterface("8bca424f16d6448ab09e57a028853aa9").then(
        (res) => {
          this.pollpersonOptions = res.data;
        }
      );
    },
    getPollpointOptions() {
      return previewDataInterface("40090a6270dc4708be9e17749c47502f").then(
        (res) => {
          this.pollpointOptions = res.data;
        }
      );
    },

    async initData() {
      this.listLoading = true;
      await this.getPollpersonOptions();
      await this.getPollpointOptions();
      let query = {
        ...this.listQuery,
        ...this.query,
      };
      request({
        url: `/api/System/PollPlan`,
        method: "get",
        data: query,
      }).then((res) => {
        let array = res.data.list;
        let k_array = this.pollpointOptions;
        let p_array = this.pollpersonOptions;
        if (Array.isArray(array)) {
          array.forEach((t, i) => {
            t.pollpoint.forEach((j, o) => {
              k_array.forEach((k) => {
                if (k.Id == j) {
                  array[i].pollpoint[o] = k.DWMC;
                }
              });
            });
            t.pollperson.forEach((j, o) => {
              p_array.forEach((k) => {
                if (k.F_Id == j) {
                  array[i].pollperson[o] = k.F_RealName;
                }
              });
            });
          });
        }

        this.list = array;
        this.total = res.data.pagination.total;
        this.listLoading = false;
        this.$nextTick(() => {
          this.tableHeight = "100%";
        });
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/System/PollPlan/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JNPFForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query };
      request({
        url: `/api/System/PollPlan/Actions/Export`,
        method: "POST",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        window.location.href = this.define.comUrl + res.data.url;
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,

        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,

        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
  },
};
</script>